<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>接口签名示例</title>
    <script src="./js/jquery.min.js"></script>
    <script src="./js/crypto-js.js"></script>
</head>
<body>

</body>

<script>

    function generateSign(params, key) {
        // 对参数按照key=value的格式，并按照参数名ASCII字典序排序
        var str = Object.keys(params).sort().map(function (key) {
            return key + '=' + params[key] + '&';
        }).join('');

        // 拼接key
        str += 'key=' + key;

        // 使用md5加密
        return CryptoJS.MD5(str).toString();
    }


    $(document).ready(function () {
        var appid = 'appid'; // 请替换为您的appid，不要泄露appid
        var key = 'appkey'; // 请替换为您的密钥，不要泄露密钥
        var basePath = 'http://localhost:18080/game-api'; // 替换接口host

        // 生成公共参数
        var commonParams = {
            appid: appid,
            timestamp: Date.now(),
            nonce: Math.random().toString(36).substr(2),
        }

        // 生成接口参数，此处以穿越封锁线为例
        var gameParams = {
            uid: '1902630302277779458',
            level: 'D',
            duration: 150,
            bump: 2,
            score: 80,
            radar:80
        }

        // 合并参数
        var params = Object.assign({}, commonParams, gameParams);

        // 生成签名
        var sign = generateSign(params, key);

        // 添加签名
        params['sign'] = sign;

        // 发送请求
        $.ajax({
            url: basePath + '/sstm/biz/prod/cyfsx/upload', //测试穿越封锁线上报地址
            type: 'POST',
            contentType: 'application/json',
            data: JSON.stringify(params),
            success: function (response) {
                console.log('Response:', response);
            },
            error: function (xhr, status, error) {
                console.error('Error:', error);
            }
        });
    });
</script>
</html>